package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.annotation.TokenValidSkip;
import com.harmontronics.hanma.paas.paas.domain.organization.service.IHmOrgService;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.dto.organization.output.HmOrgResult;
import com.harmontronics.hanma.paas.paas.dto.organization.input.HmOrgCreateInput;
import com.harmontronics.hanma.paas.paas.dto.organization.input.HmOrgUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.organization.entity.HmOrg;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 组织架构
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-01 14:45:15
 */
@RestController
@RequestMapping("/api/hmorg")
@Api(tags = {"组织架构"})
public class HmOrgController {

    @Autowired
    private IHmOrgService hmOrgAppService;


    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    @TokenValidSkip
    public ResponseEntity<List<HmOrgResult>> list() {
        List<HmOrg> hmOrgList = hmOrgAppService.getAll();

        return ResponseEntity.ok(Convert.toList(HmOrgResult.class, hmOrgList));
    }


    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmOrgCreateInput hmOrgInput,BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmOrg hmOrg =BeanUtil.toBean(hmOrgInput,HmOrg.class);
        hmOrgAppService.save(hmOrg);

        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmOrgUpdateInput hmOrgInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmOrg hmOrg =BeanUtil.toBean(hmOrgInput,HmOrg.class);
        hmOrgAppService.update(hmOrg);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestParam Long id) {
        hmOrgAppService.delete(id);
        return ResponseEntity.ok();
    }


    @GetMapping("/validate-field")
    @ApiOperation(value = "重名检查")
    public ResponseEntity<String>  validateField(
            @RequestParam(required = false) Long id,
            @RequestParam String value,
            @RequestParam(required = false) Long parentId) {
        hmOrgAppService.validateField(id, value, parentId);
        return ResponseEntity.ok();
    }

    @PutMapping(value = "/up")
    @ApiOperation(value = "组织树节点上移")
    public ResponseEntity<String> up(@RequestBody HmOrgUpdateInput hmOrgInput) {
        hmOrgAppService.up(hmOrgInput.getId());
        return ResponseEntity.ok();
    }

    @PutMapping(value = "/down")
    @ApiOperation(value = "组织树节点下移")
    public ResponseEntity<String> down(@RequestBody HmOrgUpdateInput hmOrgInput) {
        hmOrgAppService.down(hmOrgInput.getId());
        return ResponseEntity.ok();
    }

}
